<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
class SEController extends Controller {
    
    //用户修改
    public function updateSE(){
    if(session('name')){
			$seTable = M ( 'SearchEngine' );
			$map['id'] = I('param.id');
			$data=array();
			if(!empty(I('param.se_name',''))){
				$data['se_name'] = I('param.se_name','');
				$mymap['se_name'] = $data ['se_name'];
				$mymap['id'] = array('neq',I('param.id'));
				$se = $seTable->where ( $mymap )->select ();
				if ($se) {
					echo json_encode ( array (
							'result' => '名称重复',
							'code' => 0
					) );
					exit ();
				}
			}
			if(!empty(I('param.se_add',''))){
				$data['se_add'] = I('param.se_add','');
			}
			if(!empty(I('param.se_rule',''))){
				$data['se_rule'] = I('param.se_rule','');
			}
			$seTable->where($map)->save($data);
			echo json_encode(array('result'=>'修改成功','code'=>1));
		}else{
			echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
		}
    }
		
		// 用户添加
	public function addSE() {
		if (session ( 'name' )) {
			$seTable = M ( 'SearchEngine' );
			$data ['se_name'] = I ( 'param.se_name', '' );
			$data ['se_add'] = I ( 'param.se_add', '' );
			$data ['se_rule'] = I ( 'param.se_rule', '' );
			$map ['se_name'] = $data ['se_name'];
			if(empty($data ['se_name'])){
				die(json_encode ( array ('result' => '名称不能为空',	'code' => 0)));
			}
			if(empty($data ['se_add'])){
				die(json_encode ( array ('result' => '地址不能为空',	'code' => 0)));
			}
			if(empty($data ['se_rule'])){
				die(json_encode ( array ('result' => '搜索规则不能为空',	'code' => 0)));
			}
			$se = $seTable->where ( $map )->select ();
			if ($se) {
				echo json_encode ( array (
						'result' => '用户名重复',
						'code' => 0 
				) );
				exit ();
			}
			$seTable->add ( $data );
			echo json_encode ( array (
					'result' => '添加成功',
					'code' => 1 
			) );
		} else {
			echo json_encode ( array (
					'result' => '登录失效重新登陆',
					'code' => 0));
    	}
    }
    //用户删除
    public function deleteSE(){
    	if(session('name')){
    		$taskTable = M('Task');
    		$map['search_id'] = array('neq','');
    		$task = $taskTable->field('search_id')->where($map)->select();
    		if($task){
    			foreach ($task as $ta){
    				$search_id_arr = explode(',', $ta['search_id']);
    				foreach ($search_id_arr as $se_id){
    					if($se_id == I('param.id','')){
    						echo json_encode(array('result'=>'请先取消任务于当前引擎的关联','code'=>0));
    						exit();
    					}
    				}
    			}
    		}
    		$seTable = M('SearchEngine');
    		$seTable->delete(I('param.id'));
    		echo json_encode(array('result'=>'删除成功','code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    
    //用户list
    public function seList(){
    	if(session('name')){
    		$seTable = M ( 'SearchEngine' );
    		if(!empty(I('param.page','')) && !empty(I('param.pageCount',''))){
    			$seTable->page(I('param.page'),I('param.pageCount'));
    		}
    		$se = $seTable->select();
    		echo json_encode(array('result'=>$se,'count'=>$seTable->count(),'code'=>1));
    	}else{
    		echo json_encode(array('result'=>'登录失效重新登陆','code'=>0));
    	}
    }
    

}